package com.bkhc.business.service;

import com.alibaba.fastjson.JSONObject;
import com.bkhc.business.entity.*;
import com.bkhc.business.entity.vo.ApplyInfomation;
import com.bkhc.business.repository.*;
import com.bkhc.common.entity.User;
import com.bkhc.common.entity.UserRole;
import com.bkhc.common.permissions.bean.RoleDetail;
import com.bkhc.common.permissions.bean.UserPermission;
import com.bkhc.common.repository.UmcRepository;
import com.bkhc.common.repository.UserRepository;
import com.bkhc.common.repository.UserRoleRepository;
import com.bkhc.common.service.UserService;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.OrderVo;
import com.bkhc.common.vo.UserBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderService {

    @Autowired
    private OrderRepository repository;

    @Autowired
    private GroupRepository groupRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private ProductRepository productRepository;


    public PageUtil getOrdersByBussiness(OrderVo order, int pageIndex, int pageSize) {
        UserBean crruentUser = CommonUtil.getCrruentUser();
        UserPermission userPermission = userService.queryUserPermission(crruentUser.getUserId());
        List<RoleDetail> roles = userPermission.getRoles();
        if(CollectionUtils.isEmpty(roles)){
           return null;
        }
        boolean isManager = roles.stream().anyMatch(roleDetail -> "manager".equals(roleDetail.getRole_code()));
        boolean isBusiness = roles.stream().anyMatch(roleDetail -> "business".equals(roleDetail.getRole_code()));
//        boolean isAdmin = roles.stream().anyMatch(roleDetail -> "admin".equals(roleDetail.getRole_code()));
        if(isManager){
            Group group = groupRepository.findByManager(crruentUser.getUserId());
            order.setBusinessGroup(group.getGroupCode());
        }
        if(isBusiness){
            order.setBusinessGroupMember(crruentUser.getUserId());
        }
        PageUtil res = repository.getOrdersByBussiness(order, pageIndex, pageSize);
        if(!CollectionUtils.isEmpty(res.getList())){
            List<Map<String,Object>> orders= (List<Map<String,Object>>) res.getList();
            List<String> allgoodsCode = new ArrayList<>();
            for(Map<String,Object> map:orders){
                List<Map<String,Object>> orderitems= (List<Map<String,Object>>)map.get("orderitems");
                for(Map<String,Object> orderitem:orderitems){
                    allgoodsCode.add(orderitem.get("goodsCode")+"");
                }
            }
            List allspes = productRepository.getSpecsByGoodsList(allgoodsCode);
            for(Map<String,Object> map:orders){
                List<Map<String,Object>> orderitems= (List<Map<String,Object>>)map.get("orderitems");
                for(Map<String,Object> orderitem:orderitems){
                    orderitem.put("goodsSpecList",allspes.stream().filter(x->((Map)x).get("goodsCode").equals(orderitem.get("goodsCode")+"")).collect(Collectors.toList()));
                }
            }
        }
        return res;
    }
}
